﻿using MediatR;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using PersonalWebsite.Domain.Abstract.DBContext;
using PersonalWebsite.Domain.Configurations.Post;
using PersonalWebsite.Domain.Configurations.Website;
using PersonalWebsite.Domain.Entities.Post;
using PersonalWebsite.Domain.Entities.Website;

namespace PersonalWebsite.Infrastructure.DBContext;

public class PersonalWebsiteDbContext : DomainDBContext
{

    public PersonalWebsiteDbContext() { }
    public PersonalWebsiteDbContext(DbContextOptions options, IMediator mediator, IConfiguration configuration) : base(options, mediator)
    { }

    public DbSet<WS_Ability> WS_Abilities { get; set; }
    public DbSet<WS_ProExperience> WS_ProExperiences { get; set; }
    public DbSet<WS_Book> WS_Books { get; set; }    
    public DbSet<WS_GameVideo> WS_GameVideos { get; set; }
    public DbSet<WS_HotMovie> WS_HotMovies { get; set; }
    public DbSet<WS_Show> WS_Shows { get; set; }
    public DbSet<WS_TgaGame> WS_TgaGames { get; set; }
    public DbSet<WS_Project> WS_Projects { get; set; }

    public DbSet<WP_Post> WP_Posts { get; set; }

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.ApplyConfiguration(new WS_AbilityConfiguration());
        modelBuilder.ApplyConfiguration(new WS_ProExperienceConfiguration());
        modelBuilder.ApplyConfiguration(new WS_BookConfiguration());
        modelBuilder.ApplyConfiguration(new WS_GameVideoConfiguration());
        modelBuilder.ApplyConfiguration(new WS_HotMovieConfiguration());
        modelBuilder.ApplyConfiguration(new WS_ShowConfiguration());
        modelBuilder.ApplyConfiguration(new WS_TgaGameConfiguration());
        modelBuilder.ApplyConfiguration(new WS_ProjectConfiguration());

        modelBuilder.ApplyConfiguration(new WP_PostConfiguration());

        base.OnModelCreating(modelBuilder);
    }

    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    {
        if (!optionsBuilder.IsConfigured)
        {
            string connectionString = "Server=127.0.0.1;Database=PersonalWebsite;Port=3306;charset=utf8;uid=root;pwd=@!postive6617;";

            optionsBuilder.UseMySql(connectionString, ServerVersion.AutoDetect(connectionString));
        }

        base.OnConfiguring(optionsBuilder);
    }
}
